package com.bigzero.workflow.modules.process.controller;

import com.bigzero.workflow.common.dto.DeployProcessDto;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.common.model.TableDataInfo;
import com.bigzero.workflow.modules.process.query.DefinitionQuery;
import com.bigzero.workflow.modules.process.service.DefinitionService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 流程定义前端控制器
 * @author fanxinxiong
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/process/definition")
@Tag(name = "流程管理-流程定义")
public class DefinitionController {

    @Autowired
    private DefinitionService processDefinitionService;

    /**
     * 列表
     * @param definitionQuery 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("hasAuthority('process:definition:list')")
    @GetMapping("list")
    public TableDataInfo list(DefinitionQuery definitionQuery) {
        return processDefinitionService.queryPage(definitionQuery);
    }

    /**
     * 获取流程定义xml
     * @param deploymentId 部署id
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "获取流程定义xml")
    @GetMapping("getDefinitionXml")
    public R<String> getDefinitionXml(String deploymentId) {
        String xml = processDefinitionService.getDefinitionXml(deploymentId);
        return R.ok(xml);
    }

    /**
     * 获取流程定义详情
     * @param deploymentId 部署id
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "获取流程定义详情")
    @GetMapping("getDefinitionInfo")
    public R<Map<String, Object>> getDefinitionInfo(String deploymentId) {
        Map<String, Object> result = processDefinitionService.getDefinitionInfo(deploymentId);
        return R.ok(result);
    }

    /**
     * 更新流程定义状态 激活或者挂起
     * @param deploymentId 部署id
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 4)
    @Operation(summary = "更新流程定义状态 激活或者挂起")
    @GetMapping("updateState")
    public R<String> updateState(String deploymentId) {
        processDefinitionService.updateState(deploymentId);
        return R.ok();
    }

    /**
     * 部署流程
     * @param dto 参数
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 5)
    @Operation(summary = "部署流程")
    @PostMapping("deployProcess")
    public R<String> deployProcess(@Valid @RequestBody DeployProcessDto dto) {
        processDefinitionService.deployProcess(dto);
        return R.ok();
    }

    /**
     * 删除
     * @param id 主键
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 6)
    @Operation(summary = "删除")
    @DeleteMapping("delete")
    public R<String> delete(@RequestBody String id) {
        processDefinitionService.delete(id);
        return R.ok();
    }
}
